class Share {
    constructor($modalInstance, $rootScope, imageSrv, userSrv, ctgNotification, tabId,userCodeOfShare) {
        "ngInject"

        this.title = "共享";
        this._modalInstance = $modalInstance;
        this._userSrv = userSrv;
        this._imageSrv = imageSrv;
        this._ctgNotification = ctgNotification;
        this._user = $rootScope.user;
        this._tabId = tabId;
        this._userCodeOfShare = userCodeOfShare;
        // console.log($rootScope.user)
        this.tabledata = [];

        this.page = {
            currentPage: 1,
            totalItems: 0
        }
        this.getList();
    }
    cancel() {
        this._modalInstance.dismiss();
    }
    ok() {
        this.ids = [];
        this.tabledata.forEach((dat, ind) => {
            if (dat.checked) {

                this.ids.push(dat.sysUserId);
            }
        });
        this._modalInstance.close(this.ids);
    }
    getList() {


        this.loading = true;
        this._userSrv.getUserList({ currentPage: this.page.currentPage }).then((data) => {
                var _count = 0;
                data.list = data.list.filter((item, index) => {

                    if (item.userCode == this._user.sysUserCode) {
                        _count++;
                    } else {
                        return true;
                    }

                })
                var userCodeOfShar = this._userCodeOfShare;
                function filterByShare(item) {
                    if ( item.userCode != userCodeOfShar) {
                      return true;
                    }
                    return false;
                    }
                data.list=data.list.filter(filterByShare);

                this._imageSrv.getShareImage({ dockerImageTagId: this._tabId }).then((dat) => {

                        angular.forEach(dat, (its, ind) => {

                            data.list.forEach((ta, taind) => {
                                if (ta.userCode === its.code) {
                                    ta.checked=true;
                                }
                            });
                        });
                         this.tabledata=data.list;
                        this.page.totalItems = data.total - _count;
                    }).catch(() => {
                        this._ctgNotification.error('获取已共享用户失败');
                    })
                    .finally(() => {
                        this.loading = false;

                    })
            })
            .catch(() => {
                this._ctgNotification.error('获取用户列表失败。');
            })

    }
     checked(user){
        user.checked=!user.checked;
    }
}

export default Share;
